/**
 * Project Name:mms
 * File Name:MeetingInfoServiceImpl
 * Package Name:com.scb.dev.mms.service.impl
 * Date:2018/12/217:59
 * Copyright (c) 2018, Yuchuan.Dong All Rights Reserved.
 */
package com.scb.dev.mms.service.impl;

import com.scb.dev.mms.common.ResponseResult;
import com.scb.dev.mms.common.ResultType;
import com.scb.dev.mms.common.StatusCode;
import com.scb.dev.mms.dao.MeetingInfoMapper;
import com.scb.dev.mms.dao.MeetingRoomMapper;
import com.scb.dev.mms.dto.BookMeetingDto;
import com.scb.dev.mms.dto.InsertMeetingDto;
import com.scb.dev.mms.pojo.MeetingInfo;
import com.scb.dev.mms.pojo.MeetingRoom;
import com.scb.dev.mms.service.IMeetingInfoService;
import com.scb.dev.mms.service.IMeetingRoomService;
import com.scb.dev.mms.vo.BookMeetingInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * ClassName: MeetingInfoServiceImpl <br/>
 * @Description: ${todo} ADD REASON(可选). <br/><br/>
 * @Date: 2018/12/2 17:59 <br/> 
 *
 * @author Yuchuan.Dong
 * @version V1.0
 * @since JDK 1.8
 */
@Slf4j
@Service
public class MeetingInfoServiceImpl implements IMeetingInfoService {
    @Resource
    private MeetingInfoMapper meetingInfoMapper;

    @Resource
    private MeetingRoomMapper meetingRoomMapper;

    @Override
    public ResponseResult insertMeetingInfo(InsertMeetingDto insertMeetingDto) {
        if (insertMeetingDto != null) {
            log.info("The parameter is empty. The job title query failed");
            return new ResponseResult(false, ResultType.PARAMETER_NULL, StatusCode.PARAMETERNULL);
        }
        MeetingInfo meetingInfo = new MeetingInfo();
        meetingInfo.setMeeCreateTime(insertMeetingDto.getCreateName());
        meetingInfo.setMeeRoomId(insertMeetingDto.getMeeRoomId());
        meetingInfo.setMeeName(insertMeetingDto.getMeeName());
        meetingInfo.setMeeIntro(insertMeetingDto.getMeeIntro());
        meetingInfo.setMeeRemarks(insertMeetingDto.getMeeRemarks());
        meetingInfo.setMeeStartTime(insertMeetingDto.getMeeStartTime());
        meetingInfo.setMeeEndTime(insertMeetingDto.getMeeEndTime());
        meetingInfo.setMeeState("0");
        if (meetingInfoMapper.insertSelective(meetingInfo) == 0){
            log.info("Scheduled meeting execution failed");
            return new ResponseResult(false, ResultType.ADD_ERROR, StatusCode.ADDERROR);
        }
        return new ResponseResult(true, ResultType.OPERATION_SUCCESS, StatusCode.OPERATIONSUCCESS);
    }

    @Override
    public ResponseResult queryBookMeetingInformation(String creatorName) {
        List<MeetingInfo> meetingInformationList = meetingInfoMapper.selectMeetingInfoByCreatorName(creatorName);
        List<BookMeetingInfoVo> bookMeetingInfoVos = new ArrayList<>();
        if (CollectionUtils.isEmpty(meetingInformationList)){
            return new ResponseResult(null, true, ResultType.FIND_SUCCESS, StatusCode.FINDSUCCESS);
        }
        for(MeetingInfo meetingInfo : meetingInformationList){
            bookMeetingInfoVos.add(assembleBookMeetingInfoListVo(meetingInfo));
        }
        return new ResponseResult(bookMeetingInfoVos, true, ResultType.OPERATION_SUCCESS, StatusCode.OPERATIONSUCCESS);
    }


    private BookMeetingInfoVo assembleBookMeetingInfoListVo(MeetingInfo meetingInfo){
        BookMeetingInfoVo bookMeetingInfoVo = new BookMeetingInfoVo();
        bookMeetingInfoVo.setMeeId(meetingInfo.getMeeId());
        bookMeetingInfoVo.setMeeName(meetingInfo.getMeeName());
        MeetingRoom meetingRoom = meetingRoomMapper.selectByPrimaryKey(meetingInfo.getMeeRoomId());
        bookMeetingInfoVo.setMeetingRoomId(meetingRoom.getMeeRoomId());
        bookMeetingInfoVo.setMeeRoomNum(meetingRoom.getMeeRoomNum());
        bookMeetingInfoVo.setMeeRoomName(meetingRoom.getMeeRoomName());
        bookMeetingInfoVo.setMeeStartTime(meetingInfo.getMeeStartTime());
        bookMeetingInfoVo.setMeeEndTime(meetingInfo.getMeeEndTime());
        return bookMeetingInfoVo;
    }
}
